package com.bianguo.android.apushdemo.view.activity;

import android.graphics.Color;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.design.widget.NavigationView;
import android.support.v4.view.ViewCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bianguo.android.apushdemo.R;
import com.bianguo.android.apushdemo.entity.XmlData;
import com.bianguo.android.apushdemo.utils.GlideUtil;
import com.bianguo.android.apushdemo.utils.MyLog;
import com.bianguo.android.apushdemo.utils.ScreenUtil;
import com.bianguo.android.apushdemo.utils.XmlUtil;
import com.bianguo.android.apushdemo.view.wiget.ZoomScrollView;

import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import org.xmlpull.v1.XmlSerializer;

import java.io.IOException;
import java.io.StringWriter;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;

import butterknife.BindView;
import okhttp3.ResponseBody;

public class MeActivity extends BaseActivity {

    @BindView(R.id.me_banner)
    ImageView imageView;
    @BindView(R.id.me_navigation)
    NavigationView mNavigationView;
    @BindView(R.id.me_scroll)
    ZoomScrollView mScrollView;
    @BindView(R.id.me_title)
    TextView titleTv;
    @BindView(R.id.me_text)
    TextView textTv;

    private final String IMAGE_URL = "http://sc.bianguo.com.cn/scnews/Uploads/avatar/2017-06-21/594a333991c84.jpg";

    @Override
    public int layoutId() {
        return R.layout.activity_me;
    }

    @Override
    public void initView(@Nullable Bundle savedInstanceState) {
        GlideUtil.loadImage(mContext, imageView, IMAGE_URL, 0);

        View headerView = mNavigationView.inflateHeaderView(R.layout.head_navigation_layout);
        TextView nicknameTv = (TextView) headerView.findViewById(R.id.head_navigation_nickname);
        nicknameTv.setText("九龙詹君玺");

        final int headHeight = ScreenUtil.instance(mContext).dip2px(130);

        mScrollView.addOnScrollChangedListener(new ZoomScrollView.OnScrollChangedListener() {
            @Override
            public void onScrollChanged(int x, int y, int oldX, int oldY) {
                if (y <= 0) {
                    titleTv.setBackgroundColor(Color.argb((int) 0, 227, 29, 26));//AGB由相关工具获得，或者美工提供
                } else if (y > 0 && y <= headHeight) {
                    float scale = (float) y / headHeight;
                    float alpha = (255 * scale);
                    // 只是layout背景透明(仿知乎滑动效果)
                    titleTv.setBackgroundColor(Color.argb((int) alpha, 227, 29, 26));
                } else {
                    titleTv.setBackgroundColor(Color.argb((int) 255, 227, 29, 26));
                }
            }
        });
        HashMap<String, String> map = new HashMap();
        map.put("name", "张三");
        map.put("age", "23");
        map.put("sex", "男");
        map.put("major", "老师");
        map.put("like", "玩游戏");
        textTv.setText(XmlUtil.map2Xml(map));
    }

    //设置背景透明度
    private void setBgAlphaChange(int oldy, int totalHight) {
        /**
         * 渐变取值范围0--255
         *                               高度（向上或向下移动的高度）
         * 渐变百分比： percent =--------------------------------------------
         *                           总高度（也就是head图片高度-标题栏高度）
         */
        float percent = (float) Math.abs(oldy) / Math.abs(totalHight);
        int alpha = (int) (percent * 255);
        int imgAlpha = 255 - alpha;
        titleTv.getBackground().setAlpha(alpha);
        imageView.setAlpha(imgAlpha);
    }
}
